
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="Python">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>decorators &#8212; deepaugment 0.2.0 documentation</title>
    <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
   
  <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for decorators</h1><div class="highlight"><pre>
<span></span><span class="c1"># (C) 2019 Baris Ozmen &lt;hbaristr@gmail.com&gt;</span>

<span class="sd">&quot;&quot;&quot;Module consists decorators for helping other functions of the project</span>

<span class="sd">    Decorators are arranged into following categories:</span>

<span class="sd">        1. Decorator decorators:</span>
<span class="sd">            Those augment capabilites of other decorators</span>
<span class="sd">            Medics</span>

<span class="sd">        2. Reporters:</span>
<span class="sd">            Those print or log information about functions</span>

<span class="sd">        3. Middlemen:</span>
<span class="sd">            Those manipulate information entering to (arguments) and/or</span>
<span class="sd">            exiting from (returns) functions.</span>

<span class="sd">        4. Watchmen:</span>
<span class="sd">            Those check information entering to and exiting from functions.</span>
<span class="sd">            If they see an argument and/or return not obeying to rules,</span>
<span class="sd">            throw exception.</span>

<span class="sd">        5. Classy-men:</span>
<span class="sd">            They decorate classes fashionably</span>

<span class="sd">&quot;&quot;&quot;</span>


<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">wraps</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">importlib</span> <span class="k">import</span> <span class="n">reload</span>


<span class="c1">###########################################################</span>
<span class="c1">#                   Decorator decorators</span>
<span class="c1">###########################################################</span>


<div class="viewcode-block" id="decorator_with_argumets"><a class="viewcode-back" href="../lib.decorators.html#decorators.decorator_with_argumets">[docs]</a><span class="k">def</span> <span class="nf">decorator_with_argumets</span><span class="p">(</span><span class="n">decorator</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Decorator of decorator allows the decorator it decorates to accept arbitrary</span>
<span class="sd">    number of arguments.</span>
<span class="sd">    </span>
<span class="sd">    It also allows the decorator to be used without arguments. Possible ways</span>
<span class="sd">    of calling the (decorated) decorator are:</span>
<span class="sd">        1. @decorator()</span>
<span class="sd">        2. @decorator(&lt;args&gt;)</span>
<span class="sd">        </span>
<span class="sd">    However, (decorated) decorator cannot be called without parantheses (e.g. @decorator)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">decorator_maker</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nd">@wraps</span><span class="p">(</span><span class="n">decorator</span><span class="p">)</span>
        <span class="k">def</span> <span class="nf">decorator_wrapper</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">decorator</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">decorator_wrapper</span>

    <span class="k">return</span> <span class="n">decorator_maker</span></div>


<span class="c1">###########################################################</span>
<span class="c1">#                       Reporters</span>
<span class="c1">###########################################################</span>


<div class="viewcode-block" id="Reporter"><a class="viewcode-back" href="../lib.decorators.html#decorators.Reporter">[docs]</a><span class="k">class</span> <span class="nc">Reporter</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Reporter class keeps decorators gives information about functions without intervening to them.</span>

<span class="sd">    They are designed to be used safely with functions without worrying about if they will</span>
<span class="sd">    affect the inner working of a function. They don&#39;t touch to functions, only reports.</span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="Reporter.logger"><a class="viewcode-back" href="../lib.decorators.html#decorators.Reporter.logger">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="nd">@decorator_with_argumets</span>
    <span class="k">def</span> <span class="nf">logger</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">logfile_dir</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Decorator logs the arguments and kwargs of the function it decorates.</span>

<span class="sd">        It logs to the file whose path is given by `logfile_dir` argument. If it is not given,</span>
<span class="sd">        it creates a new file named `&lt;function-name&gt;.log` in the working directory.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">import</span> <span class="nn">logging</span>

        <span class="n">reload</span><span class="p">(</span><span class="n">logging</span><span class="p">)</span>  <span class="c1"># ensures it works with Jupyter IPython</span>
        <span class="c1"># see https://stackoverflow.com/questions/18786912/</span>

        <span class="k">if</span> <span class="n">logfile_dir</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="kn">import</span> <span class="nn">os</span>

            <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span>
                <span class="n">filename</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">logfile_dir</span><span class="p">,</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">.log&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)),</span>
                <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span>
                <span class="n">filename</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">.log&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">),</span> <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span>
            <span class="p">)</span>

        <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
        <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                <span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> ran with args: </span><span class="si">{}</span><span class="s2">, and kwargs: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span>
                <span class="p">)</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">wrapper</span></div>

<div class="viewcode-block" id="Reporter.timer"><a class="viewcode-back" href="../lib.decorators.html#decorators.Reporter.timer">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">timer</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Decorator prints running time of the function it decorates.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">import</span> <span class="nn">time</span>

        <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
        <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">t1</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="n">t2</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
            <span class="nb">print</span><span class="p">(</span>
                <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">()&#39;s runtime:  </span><span class="si">{}</span><span class="s2"> sec.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">((</span><span class="n">t2</span> <span class="o">-</span> <span class="n">t1</span><span class="p">),</span> <span class="mi">4</span><span class="p">))</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="n">result</span>

        <span class="k">return</span> <span class="n">wrapper</span></div>

<div class="viewcode-block" id="Reporter.matrix_gossiper"><a class="viewcode-back" href="../lib.decorators.html#decorators.Reporter.matrix_gossiper">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">matrix_gossiper</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Gossips about the input matrices of the function it decorates.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
        <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="nb">print</span><span class="p">(</span>
                <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">()&#39;s is inputted matrices with shapes:  </span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span>
                <span class="p">)</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">wrapper</span></div>

<div class="viewcode-block" id="Reporter.counter"><a class="viewcode-back" href="../lib.decorators.html#decorators.Reporter.counter">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">counter</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Decorator counts and logs number of times the function it decorates has been called.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
        <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">wrapper</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="n">wrapper</span><span class="o">.</span><span class="n">count</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">() call counter:  </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">wrapper</span><span class="o">.</span><span class="n">count</span><span class="p">))</span>
            <span class="k">return</span> <span class="n">result</span>

        <span class="n">wrapper</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">return</span> <span class="n">wrapper</span></div></div>


<span class="c1">###########################################################</span>
<span class="c1">#                        Middlemen</span>
<span class="c1">###########################################################</span>


<div class="viewcode-block" id="multi_element_argument"><a class="viewcode-back" href="../lib.decorators.html#decorators.multi_element_argument">[docs]</a><span class="k">def</span> <span class="nf">multi_element_argument</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Decorator allows the function it decorates to work with multiple element first argument.</span>
<span class="sd">        </span>
<span class="sd">    If the first argument is a multi element type (such as list, tuple, set, or</span>
<span class="sd">    np.array), decorated function works multiple times for each, and its returnings</span>
<span class="sd">    are finally returned as a list.</span>
<span class="sd">    </span>
<span class="sd">    If the first argument is a single element type, decorated function works as usual.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">ex</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
        <span class="n">multi_element_types</span> <span class="o">=</span> <span class="p">[</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">,</span> <span class="nb">set</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">ex</span><span class="p">))]</span>
        <span class="n">args_copy</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
        <span class="n">first_arg</span> <span class="o">=</span> <span class="n">args_copy</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">first_arg</span><span class="p">)</span> <span class="ow">in</span> <span class="n">multi_element_types</span><span class="p">:</span>
            <span class="n">first_arg</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">first_arg</span><span class="p">)</span>
            <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">first_arg</span><span class="p">)):</span>
                <span class="n">new_args</span> <span class="o">=</span> <span class="p">[</span><span class="n">first_arg</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">+</span> <span class="n">args_copy</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
                <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">new_args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span>

    <span class="k">return</span> <span class="n">wrapper</span></div>


<span class="c1">###########################################################</span>
<span class="c1">#                        Watchmen</span>
<span class="c1">###########################################################</span>


<div class="viewcode-block" id="check_df_arg_nonempty"><a class="viewcode-back" href="../lib.decorators.html#decorators.check_df_arg_nonempty">[docs]</a><span class="k">def</span> <span class="nf">check_df_arg_nonempty</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Decorator checks if the DataFrame arguments of the decorated function is not empty</span>

<span class="sd">    It throws error and messages the user: if any of the argument who is a DataFrame is empty;</span>
<span class="sd">    or if no DataFrame was given</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>

        <span class="n">df_args</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">):</span>
                <span class="n">df_args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>

        <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">df_args</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;No DataFrame argument is entered&quot;</span>

        <span class="k">for</span> <span class="n">df_arg</span> <span class="ow">in</span> <span class="n">df_args</span><span class="p">:</span>
            <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">df_arg</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;One of DataFrame arguments is empty&quot;</span>

        <span class="n">rv</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">rv</span>

    <span class="k">return</span> <span class="n">wrapper</span></div>


<div class="viewcode-block" id="check_df_arg_nonempty_at"><a class="viewcode-back" href="../lib.decorators.html#decorators.check_df_arg_nonempty_at">[docs]</a><span class="nd">@decorator_with_argumets</span>
<span class="k">def</span> <span class="nf">check_df_arg_nonempty_at</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">argorder</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Decorator checks if the argument of the decorated function  is not empty.</span>

<span class="sd">    It does the same thing with &quot;check_arg_nonempty()&quot; decorator, except it checks the argument</span>
<span class="sd">    whose order is given (argorder), instead of checking the first argument</span>

<span class="sd">    Args:</span>
<span class="sd">        argorder (int): order of the given argument.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>

        <span class="n">catch</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="n">argorder</span><span class="p">]</span>
        <span class="k">assert</span> <span class="n">catch</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span> <span class="s2">&quot;args[</span><span class="si">{}</span><span class="s2">] shouldnot be None&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">argorder</span><span class="p">)</span>
        <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">catch</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;args[</span><span class="si">{}</span><span class="s2">] size should be larger than 0&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">argorder</span><span class="p">)</span>

        <span class="n">rv</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">rv</span>

    <span class="k">return</span> <span class="n">wrapper</span></div>


<span class="c1">###########################################################</span>
<span class="c1">#                   Classy-men</span>
<span class="c1">###########################################################</span>


<div class="viewcode-block" id="singleton"><a class="viewcode-back" href="../lib.decorators.html#decorators.singleton">[docs]</a><span class="k">def</span> <span class="nf">singleton</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Decorator makes sure the class it is decorating wont</span>
<span class="sd">    be instantiated more than once</span>

<span class="sd">    A usage example:</span>
<span class="sd">        @singleton</span>
<span class="sd">        class AnyClass:</span>
<span class="sd">            ...</span>

<span class="sd">    If/when the class called for second time, previous (single)</span>
<span class="sd">    instance will be returned</span>

<span class="sd">    Inspiration of the decorator comes from:</span>
<span class="sd">    https://www.python.org/dev/peps/pep-0318/#examples</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">instances</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="nd">@wraps</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">wrapper</span><span class="p">():</span>
        <span class="k">if</span> <span class="bp">cls</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">instances</span><span class="p">:</span>
            <span class="n">instances</span><span class="p">[</span><span class="bp">cls</span><span class="p">]</span> <span class="o">=</span> <span class="bp">cls</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">instances</span><span class="p">[</span><span class="bp">cls</span><span class="p">]</span>

    <span class="k">return</span> <span class="n">wrapper</span></div>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">deepaugment</a></h1>








<h3>Navigation</h3>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../index.html">Documentation overview</a><ul>
  <li><a href="index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2019, Baris Ozmen.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.8.3</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
      
    </div>

    

    
  </body>
</html>